<?php
function calculateContentPulseList(){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		//?more option
		if(!isset($_GET["next"]) || $_GET["next"]==""){
			$_GET["next"]="0";
		}
	switch($_GET["type"]){
			case "products":
				//Pulse menu for adding products, cats, etc +filter categody
					
				$query_cats = "SELECT cat_id, cat_name, (SELECT cat_name FROM kpulse_categories WHERE cat_lft < t1.cat_lft AND cat_rgt > t1.cat_rgt AND cat_id > 1 ORDER BY cat_lft LIMIT 1 ) as parent FROM kpulse_categories as t1";
				$result_cats = mysql_query($query_cats);
				
				$query_prods = "SELECT producer_id, producer_name FROM kpulse_producers";
				$result_prods = mysql_query($query_prods);

				?>
				<div>
					<a href="index.php?route=kse_admin/pulselist/add/&type=<?php echo $_GET["type"] ?>">Добавить</a>
				</div>
				<div>
					<form id="filter-form" action="index.php?route=kse_admin/pulselist/&type=products" method="GET">
					<!--<form id="filter-form" action="kse_admin/pulselist/&type=products" method="get">-->

						<input type="hidden" name="route" value="kse_admin/pulselist/" />
						<input type="hidden" name="type" value="products" />
						<label for="cat">Категория</label>
						<select id="cat" name="cat">
							<option value="0">нет</option>
						<?php
							while($row_cats = mysql_fetch_assoc($result_cats)){
								if(isset($_GET["cat"]) && $_GET["cat"]>0 && $_GET["cat"]==$row_cats["cat_id"]){
									echo "<option value=\"". $row_cats["cat_id"] ."\" selected=\"selected\">". $row_cats["cat_name"] ."(". $row_cats["parent"] .")</option>";
								}
								else{
									echo "<option value=\"". $row_cats["cat_id"] ."\">". $row_cats["cat_name"] ."(". $row_cats["parent"] .")</option>";
								}
							}
						?>
						</select>
						
						<label for="producer">Производитель</label>
						<select id="producer" name="producer">
							<option value="0">нет</option>
						<?php
							while($row_prods = mysql_fetch_assoc($result_prods)){
								if(isset($_GET["producer"]) && $_GET["producer"]>0 && $_GET["producer"]==$row_prods["producer_id"]){
									echo "<option value=\"". $row_prods["producer_id"] ."\" selected=\"selected\">". $row_prods["producer_name"] ."</option>";
								}
								else{
									echo "<option value=\"". $row_prods["producer_id"] ."\">". $row_prods["producer_name"] ."</option>";
								}
							}
						?>
						</select>
						<input type="submit" value="Отфильтровать"/>
					</form>
				</div>
				<?php
				$query = "SELECT product_id FROM kpulse_products";
				if(isset($_GET["cat"]) && $_GET["cat"]>0){
					$query = "SELECT SQL_NO_CACHE kpulse_products.*
					FROM kpulse_categories AS parent,  kpulse_categories AS node,
					kpulse_products
					WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
					AND parent.cat_id = '". addslashes($_GET["cat"]) ."'
					AND node.cat_id = kpulse_products.product_cat_id
					AND node.cat_visible=1
					AND kpulse_products.product_publicate=1
					ORDER BY parent.cat_lft";
				}
				if(isset($_GET["producer"]) && $_GET["producer"]>0){
					$query = "SELECT product_id FROM kpulse_products WHERE product_producer_id=". $_GET["producer"];
				}
				
				if(isset($_GET["cat"]) && $_GET["cat"]>0 && isset($_GET["producer"]) && $_GET["producer"]>0){
					$query = "SELECT SQL_NO_CACHE kpulse_products.*
					FROM kpulse_categories AS parent,  kpulse_categories AS node,
					kpulse_products
					WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
					AND kpulse_products.product_producer_id = ". addslashes($_GET["producer"]) ."
					AND parent.cat_id = ". addslashes($_GET["cat"]) ."
					AND node.cat_id = kpulse_products.product_cat_id
					AND node.cat_visible=1
					AND kpulse_products.product_publicate=1
					ORDER BY parent.cat_lft";
				}
				//echo $query;
				$qresult = mysql_query($query);
				$quantity = mysql_num_rows($qresult);
				echo $quantity;
				if($quantity > 80){
					$url = domain_path ."index.php?route=kse_admin/pulselist/&type=products";
					if(isset($_GET["cat"]) && $_GET["cat"]>0){
						$url = domain_path ."index.php?route=kse_admin/pulselist/&type=products&cat=". $_GET["cat"];
					}
					if(isset($_GET["producer"]) && $_GET["producer"]>0){
						$url = domain_path ."index.php?route=kse_admin/pulselist/&type=products&producer=". $_GET["producer"];
					}
					
					if(isset($_GET["cat"]) && $_GET["cat"]>0 && isset($_GET["producer"]) && $_GET["producer"]>0){
						$url = domain_path ."index.php?route=kse_admin/pulselist/&type=products&producer=". $_GET["producer"] ."&cat=". $_GET["cat"];
					}
					
					
					echo "<ul class=\"nextlinks\">";
					if(isset($_GET["next"]) && $_GET["next"]!="" && $_GET["next"] > 1){
						echo "<li><a href=\"". $url ."&next=". ($_GET["next"] - 80) ."\">&#x21e6;Назад</a></li>";
					}
					$n=1;
					for($i=0; $i<= $quantity; $i=$i+80){
						if($_GET["next"] <= $quantity){
							echo $_GET["next"]!= $i ? "<li><a href=\"". $url ."&next=". $i ."\">". $n ."</a></li>" : "<li>". $n ."</li>";
							$n++;
						}
					}
					if(isset($_GET["next"]) && $_GET["next"]!="" && $quantity > ($_GET["next"] +80)){
						echo "<li><a href=\"". $url ."&next=". ($_GET["next"] + 80) ."\">Вперед&#x21e8;</a></li>";
					}
					echo "</ul><br>";
				}
						
				echo "<form>
				<table class='sortable'>
					<thead>
						<tr>
							<th>Название</th>
							<th>Производитель</th>
							<th>Категория</th>
							<th>Цена</th>
							<th>Публ.</th>
							<th>Рекоменд.</th>
							<th>Удалить</th>
						</tr>
					</thead>
					<tbody>";
					$query = "SELECT product_id,
									 product_name,
									 (SELECT producer_name FROM kpulse_producers WHERE producer_id=product_producer_id) as producer,
									 (SELECT cat_name FROM kpulse_categories WHERE cat_id=product_cat_id) as category,
									 product_publicate,
									 product_price,
									 product_recomended
								FROM kpulse_products LIMIT ". $_GET["next"] .",80";
					
					
					if(isset($_GET["cat"]) && $_GET["cat"]>0){
						$query = "SELECT SQL_NO_CACHE kpulse_products.*, node.cat_name as category, kpulse_producers.producer_name as producer
						FROM kpulse_categories AS parent,  kpulse_categories AS node,
						kpulse_products, kpulse_producers
						WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
						AND parent.cat_id = '". addslashes($_GET["cat"]) ."'
						AND node.cat_id = kpulse_products.product_cat_id
						AND node.cat_visible=1
						AND kpulse_products.product_publicate=1
						ORDER BY parent.cat_lft LIMIT ". $_GET["next"] .",80";
					}
					if(isset($_GET["producer"]) && $_GET["producer"]>0){
						$query = "SELECT product_id,
									 product_name,
									 (SELECT producer_name FROM kpulse_producers WHERE producer_id=product_producer_id) as producer,
									 (SELECT cat_name FROM kpulse_categories WHERE cat_id=product_cat_id) as category,
									 product_publicate,
									 product_price,
									 product_recomended
								FROM kpulse_products WHERE product_producer_id=". $_GET["producer"] ." LIMIT ". $_GET["next"] .",80";
					}
					
					if(isset($_GET["cat"]) && $_GET["cat"]>0 && isset($_GET["producer"]) && $_GET["producer"]>0){
						echo "here";
						/*$query = "SELECT SQL_NO_CACHE kpulse_products.*, node.cat_name as category, kpulse_producers.* as producer
						FROM kpulse_categories AS parent,  kpulse_categories AS node,
						kpulse_products, kpulse_producers
						WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
						AND kpulse_products.product_producer_id = ". addslashes($_GET["producer"]) ."
						//AND kpulse_producers.producer_id = ". addslashes($_GET["producer"]) ."
						AND parent.cat_id = ". addslashes($_GET["cat"]) ."
						AND node.cat_id = kpulse_products.product_cat_id
						AND node.cat_visible=1
						AND kpulse_products.product_publicate=1
						ORDER BY parent.cat_lft LIMIT ". $_GET["next"] .",80";*/
						$query = "SELECT SQL_NO_CACHE kpulse_products.*, node.cat_name as category, kpulse_producers.producer_name as producer 
						FROM kpulse_categories AS parent,  kpulse_categories AS node,
						kpulse_products, kpulse_producers
						WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
						AND kpulse_products.product_producer_id = ". addslashes($_GET["producer"]) ."
						AND kpulse_products.product_producer_id = kpulse_producers.producer_id
						AND parent.cat_id = ". addslashes($_GET["cat"]) ."
						AND node.cat_id = kpulse_products.product_cat_id
						AND node.cat_visible=1
						AND kpulse_products.product_publicate=1
						ORDER BY parent.cat_lft LIMIT ". $_GET["next"] .",80";
						
					}
					
					
					
					
					$result = mysql_query($query);
					while($row = mysql_fetch_assoc($result)){
						echo "<tr>
						<td><a href=\"index.php?route=kse_admin/pulseview/&element=". $row["product_id"] ."&type=". $_GET["type"] ."\">". $row["product_name"] ."</a></td>
						<td>". $row["producer"] ."</td>
						<td>". $row["category"] ."</td>
						<td>". $row["product_price"] ."</td>
						<td>";
						echo $row["product_publicate"] ? "<input type=\"checkbox\" name=\"publicate\" value=\"1\" checked=\"\">" : "<input type=\"checkbox\" name=\"publicate\" value=\"1\" >" ."</td>";
						echo "<td>";
						echo $row["product_recomended"] ? "<input type=\"checkbox\" name=\"recomended\" value=\"1\" checked=\"\">" : "<input type=\"checkbox\" name=\"recomended\" value=\"1\" >" ."</td>";
						echo "<td><a class=\"kse-menu-item\" onClick=\"kseDelelement()\"><img class=\"icons\" src=\"images/icons/delete.png\" /></a></td>
					</tr>";
					}
			break;
			case "categories":
				//Pulse menu for adding products, cats, etc
				?>
				<div>
					<a href="index.php?route=kse_admin/pulselist/add/&type=<?php echo $_GET["type"] ?>">Добавить</a>
				</div>
				<?php
				$query = "SELECT cat_id FROM kpulse_categories";			
				$qresult = mysql_query($query);
				$quantity = mysql_num_rows($qresult);
				
				if($quantity > 80){
					echo "<ul class=\"nextlinks\">";
					if(isset($_GET["next"]) && $_GET["next"]!="" && $_GET["next"] > 1){
						echo "<li><a href=\"". domain_path ."index.php?route=kse_admin/pulselist/&type=categories&next=". ($_GET["next"] - 80) ."\">&#x21e6;Назад</a></li>";
					}
					$n=1;
					for($i=0; $i<= $quantity; $i=$i+80){
						if($_GET["next"] <= $quantity){
							echo $_GET["next"]!= $i ? "<li><a href=\"". domain_path ."index.php?route=kse_admin/pulselist/&type=categories&next=". $i ."\">". $n ."</a></li>" : "<li>". $n ."</li>";
							$n++;
						}
					}
					if(isset($_GET["next"]) && $_GET["next"]!="" && $quantity > ($_GET["next"] +80)){
						echo "<li><a href=\"". domain_path ."index.php?route=kse_admin/pulselist/&type=categories&next=". ($_GET["next"] + 80) ."\">Вперед&#x21e8;</a></li>";
					}
					echo "</ul><br>";
				}
				echo "<form>
				<table class='sortable'>
					<thead>
						<tr>
							<th>Название Категории</th>
							<th>Родительская</th>
							<th>Публикация</th>
							<th>Удалить</th>
						</tr>
					</thead>
					<tbody>";
					$query = "SELECT cat_id, cat_name, cat_visible, (SELECT cat_name FROM kpulse_categories WHERE cat_lft < t1.cat_lft AND cat_rgt > t1.cat_rgt AND cat_id > 1 ORDER BY cat_lft LIMIT 1 ) as parent FROM kpulse_categories as t1 LIMIT ". $_GET["next"] .",80";
					$result = mysql_query($query);
					while($row = mysql_fetch_assoc($result)){
						echo "<tr>
						<td><a href=\"index.php?route=kse_admin/pulseview/&element=". $row["cat_id"] ."&type=". $_GET["type"] ."\">". $row["cat_name"] ."</td>
						<td>". $row["parent"] ."</td>
						<td>";
						echo $row["cat_visible"] ? "<input type=\"checkbox\" name=\"cat_visible\" value=\"1\" checked=\"\">" : "<input type=\"checkbox\" name=\"cat_visible\" value=\"1\" >" ."</td>";
						echo "<td><a class=\"kse-menu-item\" onClick=\"kseDelelement()\"><img class=\"icons\" src=\"images/icons/delete.png\" /></a></td>
					</tr>";
					}
			break;
			case "producers":
				//Pulse menu for adding products, cats, etc
				?>
				<div>
					<a href="index.php?route=kse_admin/pulselist/add/&type=<?php echo $_GET["type"] ?>">Добавить</a>
				</div>
				<?php
				$query = "SELECT producer_id FROM kpulse_producers";			
				$qresult = mysql_query($query);
				$quantity = mysql_num_rows($qresult);
				
				if($quantity > 80){
					echo "<ul class=\"nextlinks\">";
					if(isset($_GET["next"]) && $_GET["next"]!="" && $_GET["next"] > 1){
						echo "<li><a href=\"". domain_path ."index.php?route=kse_admin/pulselist/&type=producers&next=". ($_GET["next"] - 80) ."\">&#x21e6;Назад</a></li>";
					}
					$n=1;
					for($i=0; $i<= $quantity; $i=$i+80){
						if($_GET["next"] <= $quantity){
							echo $_GET["next"]!= $i ? "<li><a href=\"". domain_path ."index.php?route=kse_admin/pulselist/&type=producers&next=". $i ."\">". $n ."</a></li>" : "<li>". $n ."</li>";
							$n++;
						}
					}
					if(isset($_GET["next"]) && $_GET["next"]!="" && $quantity > ($_GET["next"] +80)){
						echo "<li><a href=\"". domain_path ."index.php?route=kse_admin/pulselist/&type=producers&next=". ($_GET["next"] + 80) ."\">Вперед&#x21e8;</a></li>";
					}
					echo "</ul><br>";
				}
				echo "<form>
				<table class='sortable'>
					<thead>
						<tr>
							<th>Название Производителя</th>
							<th>Удалить</th>
						</tr>
					</thead>
					<tbody>";
					$query = "SELECT producer_id, producer_name FROM kpulse_producers LIMIT ". $_GET["next"] .",80";
					$result = mysql_query($query);
					while($row = mysql_fetch_assoc($result)){
						echo "<tr>
						<td><a href=\"index.php?route=kse_admin/pulseview/&element=". $row["producer_id"] ."&type=". $_GET["type"] ."\">". $row["producer_name"] ."</td>
						<td><a class=\"kse-menu-item\" onClick=\"kseDelelement()\"><img class=\"icons\" src=\"images/icons/delete.png\" /></a></td>
						</tr>";
					}
			break;
			case "orders":
				$query = "SELECT order_id FROM kpulse_orders";			
				$qresult = mysql_query($query);
				$quantity = mysql_num_rows($qresult);
				
				if($quantity > 80){
					echo "<ul class=\"nextlinks\">";
					if(isset($_GET["next"]) && $_GET["next"]!="" && $_GET["next"] > 1){
						echo "<li><a href=\"". domain_path ."index.php?route=kse_admin/pulselist/&type=orders&next=". ($_GET["next"] - 80) ."\">&#x21e6;Назад</a></li>";
					}
					$n=1;
					for($i=0; $i<= $quantity; $i=$i+80){
						if($_GET["next"] <= $quantity){
							echo $_GET["next"]!= $i ? "<li><a href=\"". domain_path ."index.php?route=kse_admin/pulselist/&type=orders&next=". $i ."\">". $n ."</a></li>" : "<li>". $n ."</li>";
							$n++;
						}
					}
					if(isset($_GET["next"]) && $_GET["next"]!="" && $quantity > ($_GET["next"] +80)){
						echo "<li><a href=\"". domain_path ."index.php?route=kse_admin/pulselist/&type=orders&next=". ($_GET["next"] + 80) ."\">Вперед&#x21e8;</a></li>";
					}
					echo "</ul><br>";
				}
				echo "<form>
				<table class='sortable'>
					<thead>
						<tr>
							<th>Номер Заказа</th>
							<th>Дата Заказа</th>
							<th>Удалить</th>
						</tr>
					</thead>
					<tbody>";
					$query = "SELECT order_id, order_time FROM kpulse_orders ORDER BY order_time DESC LIMIT ". $_GET["next"] .",80";
					$result = mysql_query($query);
					while($row = mysql_fetch_assoc($result)){
						echo "<tr>
						<td><a href=\"index.php?route=kse_admin/pulseview/&element=". $row["order_id"] ."&type=". $_GET["type"] ."\">". $row["order_id"] ."</td>
						<td><a href=\"index.php?route=kse_admin/pulseview/&element=". $row["order_id"] ."&type=". $_GET["type"] ."\">". date("F j, Y, g:i a", $row["order_time"]) ."</td>
						<td><a class=\"kse-menu-item\" onClick=\"kseDelelement()\"><img class=\"icons\" src=\"images/icons/delete.png\" /></a></td>
						</tr>";
					}
			break;
		}
		echo "</tbody></table></form>";
	}
}

function calculateContentPulseView(){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		//print_r($_GET);exit();
		$submit = "<input id=\"submitbutton\" value=\"Edit\" type=\"button\" onClick=\"submit". $_GET["type"] ."()\"></div>";
		switch($_GET["type"]){
			case "products":
				$query = "SELECT *, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=product_creator_id) as creator, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=product_editor_id) as editor FROM kpulse_products WHERE product_id=". $_GET["element"];
				$result = mysql_query($query) or die(mysql_error());
									
				$row = mysql_fetch_assoc($result);
				$content = displayFormTemplatePulse($_GET["type"], $row);
			break;
			case "categories":
				$query = "SELECT *,
						 (SELECT parent.cat_rgt
							FROM kpulse_categories AS node,
							kpulse_categories AS parent
							WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
							AND node.cat_id = ". $_GET["element"] ."
							ORDER BY parent.cat_lft DESC
							LIMIT 1,1) as parent_cat_rgt,
						 (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=cat_creator_id) as creator,
						 (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=cat_editor_id) as editor

						 FROM kpulse_categories WHERE cat_id=". $_GET["element"];
				$result = mysql_query($query) or die(mysql_error());
				$row = mysql_fetch_assoc($result);
				
				$content = displayFormTemplatePulse($_GET["type"], $row);		
			break;
			case "producers":
				$query = "SELECT *, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=producer_creator_id) as creator, (SELECT kse_admin_user_name FROM kse_admin_users WHERE kse_admin_user_id=producer_editor_id) as editor FROM kpulse_producers WHERE producer_id=". $_GET["element"];
				$result = mysql_query($query) or die(mysql_error());
									
				$row = mysql_fetch_assoc($result);
				$content = displayFormTemplatePulse($_GET["type"], $row);		
			break;
			case "orders":
				$query = "SELECT * FROM kpulse_orders WHERE order_id=". $_GET["element"];
				$result = mysql_query($query) or die(mysql_error());
									
				$row = mysql_fetch_assoc($result);
				$content = displayFormTemplatePulseOrder($row);		
			break;
		}
	}
}

function calculateContentPulseAdd(){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		$content = displayFormTemplatePulse($_GET["type"]);
		return $content;
	}
}



function PulseAddContent($_POST){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		switch($_GET["type"]){
			case "products":
				echo "<pre>";
				print_r($_FILES);
				print_r($_POST);
				echo "</pre>";
				/*die();*/
				$_POST["product_publicate"] = isset($_POST["product_publicate"]) ? $_POST["product_publicate"] : 0;
				$_POST["product_recomended"] = isset($_POST["product_recomended"]) ? $_POST["product_recomended"] : 0;
				$_POST["product_description_index"] = isset($_POST["product_description_index"]) ? $_POST["product_description_index"] : 0;
				$_POST["product_display_producer"] = isset($_POST["product_display_producer"]) ? $_POST["product_display_producer"] : 0;
				
				$query = "INSERT INTO kpulse_products SET product_name='". addslashes($_POST["product_name"]) ."', 
															product_code_number='". addslashes($_POST["product_code_number"]) ."',
															product_producer_id=". $_POST["product_producer_id"] .", 
															product_publicate=". $_POST["product_publicate"] .", 
															product_recomended=". $_POST["product_recomended"] .", 
															product_cat_id=". $_POST["product_cat_id"] .",
															product_admin_rating=". $_POST["product_admin_rating"] .", 
															product_price=". $_POST["product_price"] .", 
															product_description='". addslashes($_POST["product_description"]) ."',
															product_description_index=". $_POST["product_description_index"] .",
															product_display_producer=". $_POST["product_display_producer"] .",
															product_short_des='". addslashes($_POST["product_short_des"]) ."',
															product_keywords='". addslashes($_POST["product_keywords"]) ."',
															product_page_des='". addslashes($_POST["product_page_des"]) ."',
															product_creator_id=". $_SESSION["user_id"] .",
														  	product_created=". time() .",
														  	product_editor_id=". $_SESSION["user_id"] .",
														  	product_edited=". time(); 
				
				$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
				
				$query = "SELECT product_id FROM kpulse_products WHERE product_name='". addslashes($_POST["product_name"]) ."'";
				$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
				list($productid) = mysql_fetch_row($result);

				addPhotos($_FILES["browse"], $_POST, $productid);
			break;
			case "producers":
				$query = "INSERT INTO kpulse_producers SET producer_name='". addslashes($_POST["producer_name"]) ."', 
															producer_link='". addslashes($_POST["producer_link"]) ."',
															producer_keywords='". addslashes($_POST["producer_keywords"]) ."', 
															producer_page_des='". addslashes($_POST["producer_page_des"]) ."', 
															producer_description='". addslashes($_POST["producer_description"]) ."', 
															producer_creator_id=". $_SESSION["user_id"] .",
														  	producer_created=". time() .",
															producer_editor_id=". $_SESSION["user_id"] .",
														  	producer_edited=". time(); 
				
				$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
			break;
			case "categories":
				if(substr($_POST["cat_link"], -1) == "/"){
					$_POST["cat_link"] = substr($_POST["cat_link"], 0, -1);
				}
				
				//updating cats
				if(!isset($_POST["cat_visible"]) || $_POST["cat_visible"]==""){
					$_POST["cat_visible"] = 0;
				}
								
				//adding cat parent category
				$query = "SELECT cat_rgt, cat_lft, cat_link FROM kpulse_categories WHERE cat_rgt=". $_POST["parent_cat_rgt"];
				$result = mysql_query($query) or die(mysql_error());
				//echo $query;die();
				$row = mysql_fetch_row($result);
				if(($row[0] - $row[1] < 2)){//check if done is leaf --has no childs
					//leaf
					$query = "LOCK TABLE kpulse_categories WRITE";
					$result = mysql_query($query) or die(mysql_error());
					
					$query = "SELECT cat_lft FROM kpulse_categories WHERE cat_rgt=". $_POST["parent_cat_rgt"];
					$result = mysql_query($query) or die(mysql_error());
					list($mylft) = mysql_fetch_row($result);
					
					$query = "UPDATE kpulse_categories SET cat_rgt = cat_rgt + 2 WHERE cat_rgt > ". $mylft ."";
					$result = mysql_query($query) or die(mysql_error());
					
					$query = "UPDATE kpulse_categories SET cat_lft = cat_lft + 2 WHERE cat_lft > ". $mylft ."";
					$result = mysql_query($query) or die(mysql_error());
							
					$query = "INSERT INTO kpulse_categories SET cat_name='". addslashes($_POST["cat_name"]) ."', 
																cat_title='". addslashes($_POST["cat_title"]) ."',
																cat_link='". $row[2] . addslashes($_POST["cat_link"]) ."/', 
																cat_keywords='". addslashes($_POST["cat_keywords"]) ."', 
																cat_page_des='". addslashes($_POST["cat_page_des"]) ."', 
																cat_description='". addslashes($_POST["cat_description"]) ."',
																cat_visible=". $_POST["cat_visible"] .",
																cat_editor_id=". $_SESSION["user_id"] .",
															  	cat_edited=". time() .",
															  	cat_creator_id=". $_SESSION["user_id"] .",
															  	cat_created=". time() .",
															  	cat_lft=". $mylft ." + 1,
															  	cat_rgt=". $mylft ." + 2";
					$result = mysql_query($query) or die(mysql_error());
					
					$query = "UNLOCK TABLES";
					$result = mysql_query($query) or die(mysql_error());
				}
				else{
					$query = "SELECT cat_link FROM kpulse_categories WHERE cat_rgt=". $_POST["parent_cat_rgt"];
					$result = mysql_query($query) or die(mysql_error());
					list($parent_link) = mysql_fetch_row($result);
					
					//find last subbordinate child
					$query = "SELECT node.cat_lft, (COUNT(parent.cat_link) - (sub_tree.depth + 1)) AS depth
					FROM kpulse_categories AS node,
						kpulse_categories AS parent,
						kpulse_categories AS sub_parent,
						(
							SELECT node.cat_name, (COUNT(parent.cat_link) - 1) AS depth
							FROM kpulse_categories AS node,
							kpulse_categories AS parent
							WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
							AND node.cat_rgt = '". $_POST["parent_cat_rgt"] ."'
							GROUP BY node.cat_lft
							ORDER BY node.cat_lft
						)AS sub_tree
					WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
						AND node.cat_lft BETWEEN sub_parent.cat_lft AND sub_parent.cat_rgt
						AND sub_parent.cat_name = sub_tree.cat_name
					GROUP BY node.cat_lft
					HAVING depth = 1
					ORDER BY node.cat_lft DESC LIMIT 1";
					$result = mysql_query($query) or die(mysql_error());
					$row = mysql_fetch_row($result);
	
					//INSERTING VALUES
					$query = "LOCK TABLE kpulse_categories WRITE";
					$result = mysql_query($query) or die(mysql_error());

					$query = "SELECT cat_rgt FROM kpulse_categories WHERE cat_lft = ". $row[0];
					$result = mysql_query($query) or die(mysql_error());
					list($myrgt) = mysql_fetch_row($result);
					
					$query = "UPDATE kpulse_categories SET cat_rgt = cat_rgt + 2 WHERE cat_rgt > ". $myrgt ."";
					$result = mysql_query($query) or die(mysql_error());
					
					$query = "UPDATE kpulse_categories SET cat_lft = cat_lft + 2 WHERE cat_lft > ". $myrgt ."";
					$result = mysql_query($query) or die(mysql_error());

					$query = "INSERT INTO kpulse_categories SET cat_name='". addslashes($_POST["cat_name"]) ."', 
																cat_title='". addslashes($_POST["cat_title"]) ."',
																cat_link='". $parent_link . addslashes($_POST["cat_link"]) ."/',
																cat_keywords='". addslashes($_POST["cat_keywords"]) ."', 
																cat_page_des='". addslashes($_POST["cat_page_des"]) ."', 
																cat_description='". addslashes($_POST["cat_description"]) ."',
																cat_visible=". $_POST["cat_visible"] .",
																cat_editor_id=". $_SESSION["user_id"] .",
															  	cat_edited=". time() .",
															  	cat_creator_id=". $_SESSION["user_id"] .",
															  	cat_created=". time() .",
															  	cat_lft=". $myrgt ." + 1,
															  	cat_rgt=". $myrgt ." + 2";
					$result = mysql_query($query) or die(mysql_error());
					
					$query = "UNLOCK TABLES";
					$result = mysql_query($query) or die(mysql_error());
				}
				
								
			break;
		}
	}
}

function PulseEditContent($_POST){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		switch($_GET["type"]){
			case "products":
				$_POST["product_publicate"] = isset($_POST["product_publicate"]) ? $_POST["product_publicate"] : 0;
				$_POST["product_recomended"] = isset($_POST["product_recomended"]) ? $_POST["product_recomended"] : 0;
				$_POST["product_description_index"] = isset($_POST["product_description_index"]) ? $_POST["product_description_index"] : 0;
				$_POST["product_display_producer"] = isset($_POST["product_display_producer"]) ? $_POST["product_display_producer"] : 0;
				
				$query = "UPDATE kpulse_products SET product_name='". addslashes($_POST["product_name"]) ."', 
															product_code_number='". addslashes($_POST["product_code_number"]) ."',
															product_producer_id=". $_POST["product_producer_id"] .", 
															product_publicate=". $_POST["product_publicate"] .", 
															product_recomended=". $_POST["product_recomended"] .", 
															product_cat_id=". $_POST["product_cat_id"] .",
															product_admin_rating=". $_POST["product_admin_rating"] .", 
															product_price=". $_POST["product_price"] .", 
															product_description='". addslashes($_POST["product_description"]) ."',
															product_description_index=". $_POST["product_description_index"] .",
															product_display_producer=". $_POST["product_display_producer"] .",
															product_short_des='". addslashes($_POST["product_short_des"]) ."',
															product_keywords='". addslashes($_POST["product_keywords"]) ."',
															product_page_des='". addslashes($_POST["product_page_des"]) ."',
														  	product_editor_id=". $_SESSION["user_id"] .",
														  	product_edited=". time() ."
														  	WHERE product_id=". $_GET["element"]; 
				
				$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
				
				//SETTING PHOTOS
				if(isset($_POST["photo"]) && $_POST["photo"]!=""){
					foreach($_POST["photo"] as $key => $value){
						if($_POST["defimage"] == $key){
							$catdefault = "1";
						}
						else{
							$catdefault = "0";
						}
						$query = "UPDATE kpulse_photos SET photo_alt='". addslashes($value) ."', photo_cat_logo=". $catdefault ." WHERE photo_id=". $key;
						$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
					}
				}
				//adding new photos
				addPhotos($_FILES["browse"], $_POST, $_GET["element"]);
				//addPhotos($_FILES, $_POST, $_GET["element"]);				
			break;
			case "producers":
				$query = "UPDATE kpulse_producers SET producer_name='". addslashes($_POST["producer_name"]) ."', 
															producer_link='". addslashes($_POST["producer_link"]) ."',
															producer_keywords='". addslashes($_POST["producer_keywords"]) ."', 
															producer_page_des='". addslashes($_POST["producer_page_des"]) ."', 
															producer_description='". addslashes($_POST["producer_description"]) ."', 
															producer_editor_id=". $_SESSION["user_id"] .",
														  	producer_edited=". time() ."
														  	WHERE producer_id=". $_GET["element"]; 
				
				$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
			break;
			
			case "categories":
				if(substr($_POST["cat_link"], -1) == "/"){
					$_POST["cat_link"] = substr($_POST["cat_link"], 0, -1);
				}
				
				$query = "SELECT cat_link FROM kpulse_categories WHERE cat_rgt=". $_POST["parent_cat_rgt"];
				$result = mysql_query($query) or die(mysql_error());
				list($parent_link) = mysql_fetch_row($result);
				
				
				//updating cats
				if(!isset($_POST["cat_visible"]) || $_POST["cat_visible"]==""){
					$_POST["cat_visible"] = 0;
				}
				//print_r($_POST);
				$query = "UPDATE kpulse_categories SET cat_name='". addslashes($_POST["cat_name"]) ."', 
															cat_title='". addslashes($_POST["cat_title"]) ."',
															cat_link='". $parent_link . addslashes($_POST["cat_link"]) ."/',
															cat_keywords='". addslashes($_POST["cat_keywords"]) ."', 
															cat_page_des='". addslashes($_POST["cat_page_des"]) ."', 
															cat_description='". addslashes($_POST["cat_description"]) ."',
															cat_visible=". $_POST["cat_visible"] .",
															cat_editor_id=". $_SESSION["user_id"] .",
														  	cat_edited=". time() ."
														  	WHERE cat_id=". $_GET["element"];
				$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
				
				//updating cat parent category
				
				
				$query1 = "SELECT parent.cat_rgt as parent_rgt, node.cat_rgt, node.cat_lft
				FROM kpulse_categories AS node,
				kpulse_categories AS parent
				WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
				AND node.cat_id = ". $_GET["element"] ."
				ORDER BY parent.cat_lft DESC
				LIMIT 1,1";
				$result1 = mysql_query($query1) or die(mysql_error());
				list($parentrgt, $nodergt, $nodelft) = mysql_fetch_row($result1);
				
				$querylock = "LOCK TABLE kpulse_categories WRITE";
				$result = mysql_query($querylock) or die(mysql_error());
				
				if($parentrgt != $_POST["parent_cat_rgt"]){//check if parent change
					$parentrgt = $_POST["parent_cat_rgt"];//apply new parent

					//#1 SET cat and it's subcats inactive
					$query = "UPDATE kpulse_categories SET cat_state = 1 WHERE cat_lft >=". $nodelft ." AND cat_rgt <= ". $nodergt;
					$result = mysql_query($query) or die(mysql_error());
					/*#DEBBUG
					echo "<strong>STEP 1</strong><br>";
					echo $query ."<br>"; 
					$query = "SELECT cat_name, cat_lft, cat_rgt, cat_state FROM kpulse_categories ORDER BY cat_lft";
					$result = mysql_query($query) or die(mysql_error());
					while($row = mysql_fetch_assoc($result)){
						echo $row["cat_name"] ."|". $row["cat_lft"] ."|". $row["cat_rgt"] ."|". $row["cat_state"] ."<br>";
					}
					#DEBBUG*/
					
					
					//2 Counting Special kooficient
					$k=$nodergt-$nodelft+1;
					
					/*#DEBBUG
					echo "<strong>STEP 2</strong><br>";
					echo $k ."<br>"; 
					#DEBBUG	*/
									
					//3 Moving all right part
					$query = "UPDATE kpulse_categories SET cat_lft = cat_lft - ". $k .", cat_rgt = cat_rgt - ". $k ." WHERE cat_lft > ". $nodelft ." AND cat_rgt > ". $nodergt;  
					$result = mysql_query($query) or die(mysql_error());
					
					/*#DEBBUG
					echo "<strong>STEP 3 Moving all right part</strong><br>";
					echo $query ."<br>"; 
					$query = "SELECT cat_name, cat_lft, cat_rgt, cat_state FROM kpulse_categories ORDER BY cat_lft";
					$result = mysql_query($query) or die(mysql_error());
					while($row = mysql_fetch_assoc($result)){
						echo $row["cat_name"] ."|". $row["cat_lft"] ."|". $row["cat_rgt"] ."|". $row["cat_state"] ."<br>";
					}
					#DEBBUG*/
					
					//4 Fixing Parent cats
					$query = "UPDATE kpulse_categories SET cat_rgt = cat_rgt - ". $k ." WHERE cat_lft < ". $nodelft ." AND cat_rgt > ". $nodergt;  
					$result = mysql_query($query) or die(mysql_error());

					/*#DEBBUG
					echo "<strong>STEP 4 Fixing Parent cats</strong><br>";
					echo $query ."<br>"; 
					$query = "SELECT cat_name, cat_lft, cat_rgt, cat_state FROM kpulse_categories WHERE cat_state=0 ORDER BY cat_lft";
					$result = mysql_query($query) or die(mysql_error());
					while($row = mysql_fetch_assoc($result)){
						echo $row["cat_name"] ."|". $row["cat_lft"] ."|". $row["cat_rgt"] ."<br>";
					}
					echo $parentrgt;
					echo "|".$nodergt;
					#DEBBUG*/
					
					
					//select in which side we move cats
					if($parentrgt < $nodergt){
						
						$n = $nodelft - $parentrgt;
						//moving cat and it's subcats
						$query = "UPDATE kpulse_categories SET cat_lft = cat_lft - ". $n .", cat_rgt = cat_rgt - ". $n ." WHERE cat_state = 1"; 
						$result = mysql_query($query) or die(mysql_error());
						/*#DEBBUG
						echo "<strong>STEP 5a moving cat and subcats left</strong><br>";
						echo $query ."<br>"; 
						$query = "SELECT cat_name, cat_lft, cat_rgt, cat_state FROM kpulse_categories WHERE cat_state=1 ORDER BY cat_lft";
						$result = mysql_query($query) or die(mysql_error());
						while($row = mysql_fetch_assoc($result)){
							echo $row["cat_name"] ."|". $row["cat_lft"] ."|". $row["cat_rgt"] ."<br>";
						}
						#DEBBUG*/
						
						$nodelft = $nodelft - $n;
						
					}
					else{
						//this because we move it left first
						$parentrgt = $parentrgt - $k; 
						$n = $parentrgt - $nodelft;
						//moving cat and it's subcats
						$query = "UPDATE kpulse_categories SET cat_lft = cat_lft + ". $n .", cat_rgt = cat_rgt + ". $n ." WHERE cat_state = 1"; 
						$result = mysql_query($query) or die(mysql_error());
						/*#DEBBUG
						echo "<strong>STEP 5b moving cat and subcats right</strong><br>";
						echo "\$n = \$parentrgt - \$nodelft=". $parentrgt ." - ". $nodelft ." = ". $n;
						echo $query ."<br>"; 
						$query = "SELECT cat_name, cat_lft, cat_rgt, cat_state FROM kpulse_categories WHERE cat_state=1 ORDER BY cat_lft";
						$result = mysql_query($query) or die(mysql_error());
						while($row = mysql_fetch_assoc($result)){
							echo $row["cat_name"] ."|". $row["cat_lft"] ."|". $row["cat_rgt"] ."<br>";
						}
						#DEBBUG*/
						
						$nodelft = $nodelft + $n;
					}

					//6
					
					
					$query = "UPDATE kpulse_categories SET cat_rgt = cat_rgt + ". $k ." WHERE cat_rgt >=". $nodelft ." AND cat_lft < ". $nodelft; 
					$result = mysql_query($query) or die(mysql_error());
					/*#DEBBUG
					echo "<strong>STEP 6 updating nodelft</strong><br>";
					echo $nodelft ."<br>";
					 
					$query = "SELECT cat_name, cat_lft, cat_rgt, cat_state FROM kpulse_categories ORDER BY cat_lft";
					$result = mysql_query($query) or die(mysql_error());
					while($row = mysql_fetch_assoc($result)){
						echo $row["cat_name"] ."|". $row["cat_lft"] ."|". $row["cat_rgt"] ."<br>";
					}
					#DEBBUG*/
					
					
					//7
					$query = "UPDATE kpulse_categories SET cat_lft = cat_lft + ". $k .", cat_rgt = cat_rgt + ". $k ." WHERE cat_lft >=". $nodelft ." AND cat_state=0";  
					$result = mysql_query($query) or die(mysql_error());
					/*#DEBBUG
					echo "<strong>STEP 7</strong><br>";
					echo $nodelft ."<br>";
					 
					$query = "SELECT cat_name, cat_lft, cat_rgt, cat_state FROM kpulse_categories ORDER BY cat_lft";
					$result = mysql_query($query) or die(mysql_error());
					while($row = mysql_fetch_assoc($result)){
						echo $row["cat_name"] ."|". $row["cat_lft"] ."|". $row["cat_rgt"] ."<br>";
					}
					echo "changed";
					#DEBBUG*/
					
					$query = "UPDATE kpulse_categories SET cat_state = 0";
					$result = mysql_query($query) or die(mysql_error());
				}
				$queryunlock = "UNLOCK TABLES";
				$result = mysql_query($queryunlock) or die(mysql_error());
				
			break;
		}
	}
}


/*templating function for all form-types*/
function displayFormTemplatePulse($type, $resources = null){
	if(isset($type) && $type!=""){
		switch($type){
			case "products":
				if(isset($resources["creator"])){
					?>
					<ul>
						<li>Создал: <?php echo $resources["creator"] ?></li>
						<li>Время создания: <?php echo date("F j, Y, g:i a", $resources["product_created"]) ?></li>
						<li>Отредактировал: <?php echo $resources["editor"] ?></li>
						<li>Время реадктирования: <?php echo date("F j, Y, g:i a", $resources["product_edited"]) ?></li>
					</ul>				
					<?php 
				}
				?>
				<form id="templateform" method="post" enctype="multipart/form-data" action="<?php echo domain_path ."index.php?route=kse_admin/". ((isset($resources["product_id"])) ? "pulseedit/&element=". $resources["product_id"] : "pulselist/add/") ."&type=". $type ?>">
				<label for="product_name">Название Продукта</label><input id="product_name" name="product_name" value="<?php echo $resources["product_name"] ?>" />
				<label for="product_code_number">Артикул</label><input id="product_code_number" name="product_code_number" value="<?php echo $resources["product_code_number"] ?>" />
				<label for="product_producer_id">Производитель</label>
					<select id="product_producer_id" name="product_producer_id">
					<?php 
						$query = "SELECT producer_id, producer_name FROM kpulse_producers";
						$result = mysql_query($query) or die(mysql_error());
						while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
							if($row["producer_id"] === $resources["product_producer_id"]){
								echo "<option value=\"". $row["producer_id"] ."\" selected>". $row["producer_name"] ."</option>";
							}
							else{
								echo "<option value=\"". $row["producer_id"] ."\">". $row["producer_name"] ."</option>";
							}
						}
					?>	
					</select>
				<fieldset>
					<legend>Опции Товара</legend>
					<div>
					<label for="product_publicate">Публикация</label>
					<?php
						echo $resources["product_publicate"]  ? "<input type=\"checkbox\" id=\"product_publicate\" name=\"product_publicate\" value=\"1\" checked>" : "<input type=\"checkbox\" id=\"product_publicate\" name=\"product_publicate\" value=\"1\">"; 
					?>
					</div>
					<div>
					<label for="product_recomended">Рекомендованый товар</label>
					<?php
						echo $resources["product_recomended"]  ? "<input type=\"checkbox\" id=\"product_recomended\" name=\"product_recomended\" value=\"1\" checked>" : "<input type=\"checkbox\" id=\"product_recomended\" name=\"product_recomended\" value=\"1\">"; 
					?>
					</div>
					<div>
					<label for="product_description_index">Индексация Описания</label>
					<?php
						echo $resources["product_description_index"]  ? "<input type=\"checkbox\" id=\"product_description_index\" name=\"product_description_index\" value=\"1\" checked>" : "<input type=\"checkbox\" id=\"product_description_index\" name=\"product_description_index\" value=\"1\">"; 
					?>
					</div>
					<div>
					<label for="product_display_producer">Отображение Производителя в описании</label>
					<?php
						echo $resources["product_display_producer"]  ? "<input type=\"checkbox\" id=\"product_display_producer\" name=\"product_display_producer\" value=\"1\" checked>" : "<input type=\"checkbox\" id=\"product_display_producer\" name=\"product_display_producer\" value=\"1\">"; 
					?>
					</div>
				</fieldset>
				
				<label for="product_cat_id">Категория</label>
					<select id="product_cat_id" name="product_cat_id">
					<?php 
						$query = "SELECT cat_id, cat_name, (SELECT cat_name FROM kpulse_categories WHERE cat_lft < t1.cat_lft AND cat_rgt > t1.cat_rgt AND cat_id > 1 ORDER BY cat_lft LIMIT 1 ) as parent FROM kpulse_categories as t1";
						$result = mysql_query($query) or die(mysql_error());
						while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
							if($row["cat_id"] === $resources["product_cat_id"]){
								echo "<option value=\"". $row["cat_id"] ."\" selected>". $row["cat_name"] ."(". $row["parent"] .")</option>";
							}
							else{
								echo "<option value=\"". $row["cat_id"] ."\">". $row["cat_name"] ."(". $row["parent"] .")</option>";
							}
						}
					?>	
					</select>
				<label for="product_admin_rating">Рейтинг администратора</label><input id="product_admin_rating" name="product_admin_rating" value="<?php echo isset($resources["product_admin_rating"]) ? $resources["product_admin_rating"] : 0  ?>" />
				<label for="product_price">Цена(в гривнах)</label><input id="product_price" name="product_price" value="<?php echo isset($resources["product_price"]) ? $resources["product_price"] : 0 ?>" />
				<fieldset id="photos">
				<legend>Картинки</legend>
					<?php
						if(isset($resources["product_id"])){//check if we adding product or creating new
							$query = "SELECT photo_id, photo_tiny_img, photo_cat_logo, photo_alt FROM kpulse_photos WHERE photo_product_id=". $resources["product_id"];
							$result = mysql_query($query) or die(mysql_error());
							while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
								echo $row["photo_cat_logo"] ? "<div><input type=\"radio\" name=\"defimage\" value=\"". $row["photo_id"] ."\" checked=\"checked\" />" : "<div><input type=\"radio\" name=\"defimage\" value=\"". $row["photo_id"] ."\" />";
								echo "<img src=\"images/product_images/tiny/". $row["photo_tiny_img"] ."\"><input id=\"photo-". $row["photo_id"] ."\" name=\"photo[". $row["photo_id"] ."]\" type=\"text\" value=\"". $row["photo_alt"] ."\"></div>";
								echo "<a href=\"index.php?route=kse_admin/pulselist/removephoto/&element=". $row["photo_id"] ."\">Удалить<img src=\"images/icons/delete.png\"></a>";
							}
						}
					
					?>

				<div>
					<input type="file" class="addimage" name="browse[]">
					<input type="radio" name="defimage" value="0">
					<input name="newphoto[]" type="text">
				</div>
				<div>
					<input type="file" class="addimage" name="browse[]">
					<input type="radio" name="defimage" value="1">
					<input name="newphoto[]" type="text" value="">
				</div>
				<div>
					<input type="file" class="addimage" name="browse[]">
					<input type="radio" name="defimage" value="2">
					<input name="newphoto[]" type="text" value="">
				</div>
				<div>
					<input type="file" class="addimage" name="browse[]">
					<input type="radio" name="defimage" value="3">
					<input name="newphoto[]" type="text" value="">
				</div>
				
				
				
				</fieldset>
				<label for="product_description">Описание</label><textarea id="product_description" name="product_description" rows="6" cols="90"><?php echo $resources["product_description"] ?></textarea>
				<label for="product_short_des">Краткое описание</label><textarea id="product_short_des" name="product_short_des" rows="3" cols="50"><?php echo $resources["product_short_des"] ?></textarea>
				<label for="product_keywords">Meta keywords</label><textarea id="product_keywords" name="product_keywords" rows="3" cols="50"><?php echo $resources["product_keywords"] ?></textarea>
				<label for="product_page_des">Meta description</label><textarea id="product_page_des" name="product_page_des" rows="3" cols="50"><?php echo $resources["product_page_des"] ?></textarea>
				<?php
			break;
			
			case "categories":
				if(isset($resources["creator"])){
					if(substr($resources["cat_link"], -1) == "/"){
						$resources["cat_link"] = substr($resources["cat_link"], 0, -1);
					}
					$query = "SELECT cat_link FROM kpulse_categories WHERE cat_rgt=". $resources["parent_cat_rgt"];
					$result = mysql_query($query) or die(mysql_error());
					list($parent_link) = mysql_fetch_row($result);
					?>
					<div>
					<ul>
						<li>Создал: <?php echo $resources["creator"] ?></li>
						<li>Время создания: <?php echo date("F j, Y, g:i a", $resources["cat_created"]) ?></li>
						<li>Отредактировал: <?php echo $resources["editor"] ?></li>
						<li>Время реадктирования: <?php echo date("F j, Y, g:i a", $resources["cat_edited"]) ?></li>
					</ul>				
					</div>
					<?php 
				}
				?>
				<form id="templateform" method="post" enctype="multipart/form-data" action="<?php echo domain_path ."index.php?route=kse_admin/". ((isset($resources["cat_id"])) ? "pulseedit/&element=". $resources["cat_id"] : "pulselist/add/") ."&type=". $type ?>">
				<label for="cat_name">Название Категории</label><input id="cat_name" name="cat_name" value="<?php echo $resources["cat_name"] ?>" />
				<label for="cat_title">Meta title</label><input id="cat_title" name="cat_title" value="<?php echo $resources["cat_title"] ?>" />
				<label for="cat_link">Ссылка на категорию</label><input id="cat_l
				ink" name="cat_link" value="<?php echo str_replace($parent_link, "", $resources["cat_link"]) ?>" />
				<label for="parent_cat_rgt">Родительская категория</label><select id="parent_cat_rgt" name="parent_cat_rgt">
					<?php 
						if(isset($resources["parent_cat_rgt"])){
							$query = "SELECT cat_rgt, cat_name FROM kpulse_categories";
							$result = mysql_query($query) or die(mysql_error());
							while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
								echo $row["cat_rgt"]==$resources["parent_cat_rgt"] ? "<option value=\"". $row["cat_rgt"] ."\" selected>". $row["cat_name"] ."</option>" :  "<option value=\"". $row["cat_rgt"] ."\">". $row["cat_name"] ."</option>";  
							}
						}
						else{
							$query = "SELECT cat_rgt, cat_name FROM kpulse_categories";
							$result = mysql_query($query) or die(mysql_error());
							while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
								echo "<option value=\"". $row["cat_rgt"] ."\">". $row["cat_name"] ."</option>";
							}  
						}
					?>
				</select>
				<label for="cat_keywords">Meta keywords</label><textarea id="cat_keywords" name="cat_keywords" rows="3" cols="50"><?php echo $resources["cat_keywords"] ?></textarea>
				<label for="cat_page_des">Meta description</label><textarea id="cat_page_des" name="cat_page_des" rows="3" cols="50"><?php echo $resources["cat_page_des"] ?></textarea>
				
				<fieldset>
				<legend>Настройки категорий</legend>
				<label for="cat_visible">Публикация</label>
					<?php
						echo $resources["cat_visible"]  ? "<input type=\"checkbox\" id=\"cat_visible\" name=\"cat_visible\" value=\"1\" checked>" : "<input type=\"checkbox\" id=\"cat_visible\" name=\"cat_visible\" value=\"1\">"; 
					?>	
				</fieldset>	
				<label for="cat_description">Описание на странице</label><textarea id="cat_description" name="cat_description" rows="3" cols="50"><?php echo $resources["cat_description"] ?></textarea>
				
				<?php
			break;
			case "producers":
				if(isset($resources["creator"])){
					?>
					<div>
					<ul>
						<li>Создал: <?php echo $resources["creator"] ?></li>
						<li>Время создания: <?php echo date("F j, Y, g:i a", $resources["producer_created"]) ?></li>
						<li>Отредактировал: <?php echo $resources["editor"] ?></li>
						<li>Время реадктирования: <?php echo date("F j, Y, g:i a", $resources["producer_edited"]) ?></li>
					</ul>				
					</div>
					<?php 
				}
				?>
				<form id="templateform" method="post" enctype="multipart/form-data" action="<?php echo domain_path ."index.php?route=kse_admin/". ((isset($resources["producer_id"])) ? "pulseedit/&element=". $resources["producer_id"] : "pulselist/add/") ."&type=". $type ?>">
				<label for="producer_name">Название Производителя</label><input id="producer_name" name="producer_name" value="<?php echo $resources["producer_name"] ?>" />
				<label for="producer_link">Ссылка на производителя</label><input id="producer_link" name="producer_link" value="<?php echo $resources["producer_link"] ?>" />
				<label for="producer_keywords">Meta keywords</label><textarea id="producer_keywords" name="producer_keywords" rows="3" cols="50"><?php echo $resources["producer_keywords"] ?></textarea>
				<label for="producer_description">Краткое описание</label><textarea id="producer_description" name="producer_description" rows="3" cols="50"><?php echo $resources["producer_description"] ?></textarea>
				<label for="producer_page_des">Дополнительное описание</label><textarea id="producer_page_des" name="producer_page_des" rows="3" cols="50"><?php echo $resources["producer_page_des"] ?></textarea> 
				<?php
			break;
		}
		?>
		<input id="submitbutton" value="Edit" type="Submit">
		</form>
		<?php
	}
}

//Display order function
function displayFormTemplatePulseOrder($resources = null){
	echo "<p>Номер заказа:". $resources["order_id"] ."</p>";
	echo "<p>Время оформления заказа:".  date("F j, Y, g:i a", $resources["order_time"]) ."</p>";
	echo $resources["order_content"]; 
}

function addPhotos($photoaaray, $_POST, $productid){
	
	foreach($photoaaray["name"] as $key => $imgvalue){
		if($photoaaray["error"][$key]==0 && $photoaaray["type"][$key]=="image/jpeg"){
			$result=mysql_query("SELECT max(photo_id)+1 FROM kpulse_photos");
			$res=mysql_fetch_assoc($result);
			$photo_id=$res["max(photo_id)+1"]?$res["max(photo_id)+1"]:1;
			$img=md5($photo_id.$_SERVER['REMOTE_ADDR']).".jpg";
			copy($photoaaray['tmp_name'][$key], real_path ."images/product_images/large/".$img);
			
			image_resizer($photoaaray['tmp_name'][$key], real_path ."images/product_images/tiny/".$img, 100, 0);
			image_resizer($photoaaray['tmp_name'][$key], real_path ."images/product_images/small/".$img, 350, 0);
			
			
			unlink($photoaaray['tmp_name'][$key]);
			
			if($_POST["defimage"] == $key){
				$catdefault = "1";
			}
			else{
				$catdefault = "0";
			}
			$query = "INSERT INTO kpulse_photos SET photo_product_id=". $productid .",
													photo_large_img='". $img ."',
													photo_small_img='". $img ."',
													photo_tiny_img='". $img ."',
													photo_alt='". addslashes($_POST["newphoto"][$key]) ."', photo_cat_logo=". $catdefault;
			$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
		}
	}
}

function calculateContentremovePhoto(){
	if(isset($_GET["element"]) && $_GET["element"]!=""){
		$query = "DELETE FROM kpulse_photos WHERE photo_id=". $_GET["element"];
		$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
	}	
}

//old pulse resizer
function image_resizer($tmp_name, $name, $width, $height){
	$im_data=@getimagesize($tmp_name);
	if ($im_data[2]==1 || $im_data[2]==2 || $im_data[2]==3) 
	{
  		include_once(real_path ."kse". DIRSEP ."modules". DIRSEP ."kpulse". DIRSEP ."class.img2thrumb.php");
		if($width!=0)
		{
			if($height==0)
				$height=floor($im_data[1]*$width/$im_data[0]);
			else
			{
				if((($width>$height)&&($im_data[1]>$im_data[0]))||(($width<$height)&&($im_data[1]<$im_data[0])))
				{
					$tmp=$width;
					$width=$height;
					$height=$tmp;
				}
			}
			$Img2Thumb = new Img2Thumb($tmp_name, $width,  $height, $name, 0); 
		}
		else
			@copy($tmp_name,$name);			    
	}
}

function calculateContentgenerate(){
	if(isset($_GET["type"]) && $_GET["type"]!=""){
		switch($_GET["type"]){
			case "price":
				echo "Generating XML please wait";
					$fp = fopen("price.xml", "w");
					fwrite($fp, "<price><date>2010-05-25 17:00</date><firmName>Название магазина</firmName><firmId>1234</firmId><rate>8.12</rate><categories>");
					$query ="SELECT normalcat.cat_id, normalcat.cat_name, (SELECT parent.cat_lft
					FROM kpulse_categories AS node,
					kpulse_categories AS parent
					WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
					AND node.cat_lft = normalcat.cat_lft
					ORDER BY parent.cat_lft DESC
					LIMIT 1,1) as parencatid FROM kpulse_categories as normalcat WHERE normalcat.cat_lft > 1";
					$result = mysql_query($query) or die(mysql_error() ."<br>". $query);
					while($row = mysql_fetch_assoc($result)){
						fwrite($fp, "<category><id>". $row["cat_id"] ."</id><name>". $row["cat_name"] ."</name>");
						if(isset($row["parencatid"]) && $row["parencatid"]!="" && $row["parencatid"]>1){
							fwrite($fp, "<parentId>". $row["parencatid"] ."</parentId>");
						}
						fwrite($fp, "</category>");
					}
					fwrite($fp, '</categories></price>');
					fclose($fp);
			break;
		}
	}	
	else{
		?>
		<a href="index.php?route=kse_admin/generate/&type=price">Прайс на hotline</a>
		<?php 
	}
}
?>
